#!/usr/bin/env python

# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Abstract attribute class """

from streetidentity_rp.error import DataError

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2012 Google Inc. All Rights Reserved."
__license__ = "Apache License 2.0"
__version__ = "0.2"
__status__ = "Prototype"


class Attribute(object):
    """Represents a single attribute returned by AP."""

    id_ = None
    type = None
    desc = None
    uri = None
    icon_url = None
    value = {}

    def __init__(self, attribute):
        if not isinstance(attribute, dict):
            raise DataError('invalid data format')
        self.id_ = attribute.get('id', None)
        self.type = attribute.get('type', None)
        self.desc = attribute.get('desc', None)
        self.uri = attribute.get('uri', None)
        self.icon_url = attribute.get('icon_url', None)
        self.value = attribute.get('value', {})

    def __str__(self):
        attribute = ''
        attribute += 'id: ' + (self.id_ or '')
        attribute += '\ntype: ' + (self.type or '')
        attribute += '\ndesc: ' + (self.desc or '')
        attribute += '\nuri: ' + (self.uri or '')
        attribute += '\nicon_url: ' + (self.icon_url or '')
        attribute += '\nvalue: ' + (str(self.value) or '')
        return attribute

    def __repr__(self):
        return self.__str__()


class StreetAddress(Attribute):
    """Represents a street address attribute returned by the attribute endpoint
    at AP."""

    line_1 = None
    line_2 = None
    city = None
    state = None
    country = None
    zip = None
    address_type = None

    def __init__(self, attribute):
        super(StreetAddress, self).__init__(attribute)
        self.line_1 = self.value.get('line_1', None)
        self.line_2 = self.value.get('line_2', None)
        self.city = self.value.get('city', None)
        self.state = self.value.get('state', None)
        self.country = self.value.get('country', None)
        self.zip = self.value.get('zip', None)
        self.address_type = self.value.get('address_type', None)

    def __str__(self):
        attribute = super(StreetAddress, self).__str__()
        attribute += '\nline_1: ' + (self.line_1 or '')
        attribute += '\nline_2: ' + (self.line_2 or '')
        attribute += '\ncity:' + (self.city or '')
        attribute += '\nstate:' + (self.state or '')
        attribute += '\ncountry: ' + (self.country or '')
        attribute += '\nzip: ' + (self.zip or '')
        attribute += '\naddress_type: ' + (self.type or '')
        return attribute

    def __repr__(self):
        return self.__str__()


class Gender(Attribute):
    """Represents a gender attribute returned by the attribute endpoint
    at AP."""

    gender = None

    def __init__(self, attribute):
        super(Gender, self).__init__(attribute)
        self.gender = self.value.get('gender', None)

    def __str__(self):
        attribute = super(Gender, self).__str__()
        attribute += '\ngender: ' + (self.gender or '')
        return attribute

    def __repr__(self):
        return self.__str__()


class Age(Attribute):
    """Represents an age attribute returned by the attribute endpoint at AP."""

    age = None

    def __init__(self, attribute):
        super(Age, self).__init__(attribute)
        self.age = self.value.get('age', None)

    def __str__(self):
        attribute = super(Age, self).__str__()
        attribute += '\nage: ' + (self.age or '')
        return attribute

    def __repr__(self):
        return self.__str__()
